package com.yfw.maple_leaf_store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yfw.maple_leaf_store.common.BaseContext;
import com.yfw.maple_leaf_store.common.R;
import com.yfw.maple_leaf_store.domain.AddressBook;
import com.yfw.maple_leaf_store.domain.User;
import com.yfw.maple_leaf_store.service.AddressBookService;
import com.yfw.maple_leaf_store.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: yfw
 * @date: 2022/6/14 19:43
 * com.yfw.maple_leaf_store.controller
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址信息
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        Long currentId = BaseContext.getCurrentId();
//        Long currentId = Long.valueOf("1538364171261296641");//测试
        addressBook.setUid(currentId);
        addressBook.setIsDefault(1);//默认为默认地址
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUid, BaseContext.getCurrentId());
//        queryWrapper.eq(AddressBook::getUid, Long.valueOf("1538364171261296641"));//测试
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 修改收货地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {

        addressBook.setUid(BaseContext.getCurrentId());//设置用户id
//        addressBook.setUid(Long.valueOf("1538364171261296641"));
        if (addressBook == null) {
            return R.error("请求异常");
        }
        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }

//    /**
//     * 设置默认地址
//     * 接收地址管理信息的
//     */
//    @PutMapping("/default")
//    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
////        Long currentId = Long.valueOf("1537801628956426242");
//        Long currentId = BaseContext.getCurrentId();
//        log.info("addressBook:{}", addressBook);
//        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.eq(AddressBook::getId,currentId);//根据当前用户查询
//        addressBookService.update(wrapper);
//        return R.success(addressBook);//提示修改成功，并将其回显，这样可以少查询一次
//    }

//    /**
//     * 前台组传入地址信息id
//     * 根据id查询具体某个地址信息
//     */
//    @GetMapping("/{id}")
//    public R get() {
//        Long currentId = BaseContext.getCurrentId();
//        AddressBook addressBook = addressBookService.getOne(id);
//        if (addressBook != null) {
//            return R.success(addressBook);
//        } else {
//            return R.error("没有找到该对象");
//        }
//    }

//    /**
//     * 查询指定用户的全部地址
//     */
//    @GetMapping("/list")
//    public R<List<AddressBook>> list(AddressBook addressBook) {
//        addressBook.setUid(BaseContext.getCurrentId());
//        log.info("addressBook:{}", addressBook);
//
//        //条件构造器
//        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(null != addressBook.getUid(), AddressBook::getUid, addressBook.getUid());
//
//        //SQL:select * from address_book where user_id = ? order by update_time desc
//        return R.success(addressBookService.list(queryWrapper));
//    }

//    /**
//     * 根据地址id删除用户地址
//     * 单独删除
//     * @param id
//     * @return
//     */
//    @DeleteMapping
//    public R<String> delete(Long id) {
//        log.info("{}",id);
//
//        if (id == null) {
//            return R.error("请求异常");
//        }
//        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(AddressBook::getId, id).eq(AddressBook::getUid,Long.valueOf("1538364171261296641"));
//        addressBookService.remove(queryWrapper);
//        //addressBookService.removeById(id);  感觉直接使用这个removeById不太严谨.....
//        return R.success("删除地址成功");
//    }


}

